package com.jml.spi;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.ServiceLoader;

public class SPIJdbc {
    public static void main(String[] args) throws SQLException {
        //当指定扩展类加载器就不会加载到驱动类
        Thread.currentThread().setContextClassLoader(SPIMain.class.getClassLoader().getParent());

        String url = "jdbc:mysql://localhost:3306/back001?user=root&password=1234";  //定义连接数据库的url
        Connection root = DriverManager.getConnection(url);

        //DriverManager的底层也是用了SPI机制，Driver类是在rt.jar包下属于启动类加载器加载
        //我们使用的FabricMySQLDriver是我们引入jar包下的META-INF/services/java.sql.Driver中的内容
        ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
        loadedDrivers.forEach(t->{
            System.out.println(t);
        });
    }
}
